package com.jcl.cloud.services.crawler.search.service;


import com.jcl.cloud.services.crawler.search.constant.TaskInfo;
import com.jcl.cloud.services.crawler.search.task.QcwySearchTaskHandler;
import com.jcl.cloud.services.crawler.search.task.SearchTaskHandler;
import com.jcl.cloud.services.crawler.search.task.SearchTaskHandlerWrap;
import com.jcl.cloud.services.crawler.search.task.ZlzpSearchTaskHandler;
import com.jcl.cloud.services.crawler.search.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;

@Service
public class SearchTaskManagerService {


    @Autowired
    private RedisService redisService;
    @Autowired
    private ThreadMonitorService threadMonitorService;



    public void createSearchTask(String crawlerId){


        String siteCode = this.redisService.hget("unicrawler:task:info:"+crawlerId,"siteCode");

        /*if(TaskInfo.runningTask.contains(crawlerId)){
            //放入任务锁的map中
            TaskInfo.taskLockMap.put(crawlerId,siteCode);

        }else{*/
            SearchTaskHandler searchTaskHandler = null;
            if("QCWY".equalsIgnoreCase(siteCode)){
                searchTaskHandler = (SearchTaskHandler) ContextUtil.getContext().getBean("qcwySearchTaskHandler");
            }else if("ZLZP".equalsIgnoreCase(siteCode)){
                searchTaskHandler = (SearchTaskHandler) ContextUtil.getContext().getBean("zlzpSearchTaskHandler");
            }


            this.threadMonitorService.execute(new SearchTaskHandlerWrap(searchTaskHandler,crawlerId),crawlerId);

       /*     TaskInfo.runningTask.put(crawlerId,siteCode);
        }*/
    }



}
